/**
 * 签到页面
 * author:yinht
 * date:2017年6月2日19:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	var iscroll = require("iscroll");
	var login = {
		data: {},
//		wxUserInfoObj: {},
		recommEnable: true,
		/**
		 * bindEvent 页面事件，要先用unbind解绑
		 */
		bindEvent: function() {
			$("#login_but").unbind().on("click", function() {
				login.sendParam("");
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "right"
				});
			});
			
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "right"; //从右边过来
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: nextPageFrom
				});
			});
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		sendParam:function(){
			var usercode = $(".username").val();
			var pwd = $(".userpwd").val();
			if(!usercode){
				common.showTip({
				    time:3000,
				    content:"账户名不能为空！",
				    callback:function(){
					    	//common.showTip({content:"我已经关闭"});
					    }
				});
				return ;
			}else if(usercode&&!pwd){
				common.showTip({
				    time:3000,
				    content:"密码不能为空！",
				    callback:function(){
				    	//common.showTip({content:"我已经关闭"});
				    	}
				});
				return ;
			}			
			var param = {
					usercode: usercode,
					pwd:pwd
		        }
			common.loadData({
				url: "/service/RRTPubServlet?method=login", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: login.LoginCallBack, //回调函数
				dataSource: "Login",
				cache: false
			});
		},
		initData: function() {
			var usercode = window.localStorage.getItem("usercode");
			var pwd = window.localStorage.getItem("pwd");
			if( usercode && (usercode!="") && pwd && (pwd!="")){
				$("#rem-pwd").prop("checked",true);
				$(".username").val(usercode);
				$(".userpwd").val(pwd);
			}
			
		},
		//callback function
		LoginCallBack: function(json) {
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				window.localStorage.setItem("pk_user",json.data.pk_user);
				window.localStorage.setItem("permissions",json.data.permissions);
				var loginstr = JSON.stringify(json.data);
				window.localStorage.setItem("loginmessage",loginstr);
				login.setInitialize();
				if(($("#rem-pwd").prop("checked")) == true){
					var users = $(".username").val();
					var pwd = $(".userpwd").val();
					window.localStorage.setItem("usercode",users);
					window.localStorage.setItem("pwd",pwd);	
				}else{
					window.localStorage.setItem("usercode","");
					window.localStorage.setItem("pwd","");
				}
					window.location.href = "#home";
					
			} else {
				common.showTip({
					content: json.message
				});
			}

		},
		setInitialize: function(){
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				"pk_user": pk_user

			}
			common.loadData({
				url: "/service/RRTPubServlet?method=initialize", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: login.initializeCallBack, //回调函数
				dataSource: "Login",
				cache: false
			});
		},
		initializeCallBack: function(json){
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				var initializestr = JSON.stringify(json.data);
				window.localStorage.setItem("initialize",initializestr);
				
			} else {
				common.showTip({
					content: json.message
				});
			}

		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			 document.addEventListener("deviceready", onDeviceReady, false); 
		        // PhoneGap加载完毕 
		        function onDeviceReady() { 
		            // 按钮事件 
		            document.addEventListener("backbutton", eventBackButton, false); // 返回键 
		        } 
		        // 返回键 
		        function eventBackButton() { 
		             alert('5秒内再次点击返回键将退出登录!'); 
		            document.removeEventListener("backbutton", eventBackButton, false); // 注销返回键 
		            document.addEventListener("backbutton", exitApp, false);//绑定退出事件 
		            // 3秒后重新注册 
		            var intervalID = window.setInterval(function() { 
		                    window.clearInterval(intervalID); 
		                    document.removeEventListener("backbutton", exitApp, false); // 注销返回键 
		                    document.addEventListener("backbutton", eventBackButton, false); // 返回键 
		            }, 5000);                     
		        }      
		        function exitApp(){ 
		            //返回登录页面
		        } 

			//动态加载数据
			login.initData();
//			事件绑定
			login.bindEvent();
//			路由绑定
			login.bindRout();
		}
	};

	module.exports = login;
});